import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';
export default {
  namespace: 'basicEventM',

  state: {
    selectData1: [],
    selectData2: [],
    tableData: {},
    tableData1: {},
    tableData2: {},
    tableData3: {},
    tableData4: {},
    inMainData: {},
    inBondData: {},
    camsMainData: {},
    camsBondData: {},
  },

  effects: {
    //获取发行人列表
    *findtableDta({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.getSelectData, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取外部-主体-表格数据
    *findTableData1({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findTableData1, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData1: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取外部-债项-表格数据
    *findTableData2({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findTableData2, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData2: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取内部-主体-表格数据
    *findInMainData({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findInMainData, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            inMainData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取内部-债项-表格数据
    *findInBondData({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findInBondData, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            inBondData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取CAMS-主体-表格数据
    *findCamsMainData({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findCamsMainData, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            camsMainData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取CAMS-债项-表格数据
    *findCamsBondData({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findCamsBondData, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            camsBondData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取到期表格数据
    *findTableData3({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findTableData3, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData3: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取行权表格数据
    *findTableData4({ payload, callback }, { call, put }) {
      const response = yield call(api.basicEvent.findTableData4, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData4: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    //获取我的持仓table数据---maat
    *findMyHoldData({ payload, callback }, { call, put }) {
      let response;
      let res = {
        columns: [
          { title: '产品', key: 'productName' },
          { title: '组合', key: 'portfolioInfoName' },
          { title: '数量（张）', key: 'lCurrentAmount' },
          { title: '券面金额（元）', key: 'parValueTotal' },
          { title: '市值（元）	', key: 'fullMarketValue' },
        ],
        dataSource: [],
        pageInfo: {
          total: '',
          pageSize: payload.pageSize,
          current: '',
        },
      };
      let isZG = false;
      // 判断当前环境是不是资管
      let temp = isZiGuanEvir();
      if (!!temp && temp['iv-user']) isZG = true;
      if (isZG) {
        res.columns[0].key = 'fundName';
        response = yield call(api.basicEvent.getSearchTotalZG, payload);
      } else {
        response = yield call(api.basicEvent.getSearchTotal, payload);
      }
      res.dataSource = response.data ?? [];
      res.pageInfo.total = response.total;
      res.pageInfo.current = response.current;
      callback(res);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
